********************************************************************************
********************************************************************************
* This do file replicates all Online Appendix figures and tables on historical 
* job ads: Figure B1, B2, B3, B4, and B5, and Table B1, B2, and B6
********************************************************************************
********************************************************************************

*******************************************************************************
*** Figure B1. Job ads with requirements on language proficiency. 
*** All occupations. 
*******************************************************************************

use Data/data_figures_annonser_lang_JHR, clear
collapse language_req_mentioned, by(year)
drop if year == .
sort year

twoway connected language_req_mentioned year, ///
title("", color(black)) ///
ytitle("") xtitle("") ///
yscale(range(0(0.1)0.4)) ///
ylabel(0(.1).4) ///
ylabel(,nogrid) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)

*******************************************************************************
*** Figure B2. Job ads with requirements on language proficiency, by occupation
*******************************************************************************
use Data/data_figures_annonser_lang_JHR, clear

drop if high_skill == .

label define SSYK_label 3513 "Computer professionals" 3322 "Business sales persons" 3313 "Accountants" ///
3512 "IT support technicians" 4111 "Accounting clerks" 4112 "HR assistants" 5311 "Preschool teachers" ///
8332 "Truck drivers" 5120 "Chefs" 7231 "Mechanics" 4119 "Office assistants" 5343 "Personal assistants" ///
5222 "Shop sales assistants" 4322 "Warehouse staff workers" 4222 "Customer service workers" 9412 "Waiters/waitresses" ///
9111 "Cleaners" 
label values SSYK4 SSYK_label

collapse language_req_mentioned, by(year SSYK4)
 
twoway connected language_req_mentioned year, by(SSYK4, graphregion(fcolor(white)) note("")) ///
title("")  ///
ytitle("") ///
xtitle("") ///
graphregion(color(white) fcolor(white))  ///
mcolor(black) lcolor(black) msize(small) ///
bgcolor(white)

*******************************************************************************
*** Figure B3. Language requirements in job ads and immigration. 
*******************************************************************************
use Data/data_figures_annonser_lang_JHR, clear
collapse language_req_mentioned, by(year)
drop if year == .
sort year

merge 1:1 year using Data/data_immigrants
drop if year == .
drop _merge

twoway connected language_req_mentioned year, ///
title("A: Job ads with language requirements", color(black)) ///
ytitle("") xtitle("") ///
ylabel(,nogrid) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)
graph save "Stata output\All adds Swedish.ghp", replace

twoway connected Immigrants year, ///
title("B: No. of immigrants", color(black)) ///
ytitle("") xtitle("") ///
ylabel(100000(50000)200000,nogrid format(%9.0fc)) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)
graph save "Stata output\Immigrants SWE.ghp", replace

graph combine "Stata output\All adds Swedish.ghp" "Stata output\Immigrants SWE.ghp", ///
rows(2) xcommon ///
graphregion(color(white))

********************************************************************************
*** Figure B4. Yearly change in language requirements in job ads and immigration
********************************************************************************

use Data/data_figures_annonser_lang_JHR, clear
collapse language_req_mentioned, by(year)
drop if year == .
sort year
gen Change_language_req_mentioned =  ((language_req_mentioned - language_req_mentioned[_n - 1]) / language_req_mentioned[_n - 1])

merge 1:1 year using Data/data_immigrants
drop if year == .
drop _merge

twoway connected Change_language_req_mentioned year, ///
title("A: Yearly change (%) in job ads with language requirements", color(black)) ///
ytitle("") xtitle("") ///
ylabel(,nogrid) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)
graph save "Stata output\Change_language_req_mentioned.ghp", replace

twoway connected change_immig year, ///
title("B: Yearly change (%) in no. of immigrants", color(black)) ///
ytitle("") xtitle("") ///
ylabel(,nogrid) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)
graph save "Stata output\Change_Immigrants SWE.ghp", replace

graph combine "Stata output\Change_language_req_mentioned.ghp" "Stata output\Change_Immigrants SWE.ghp", rows(2) xcommon ycommon ///
graphregion(color(white))

*******************************************************************************
*** Figure B5. Language requirements in job ads by immigrant inflows (quartile)
*******************************************************************************

use Data/data_share_foregin_2007, replace
rename Ssyk4 SSYK96
save Data/tmp_foregin_2007, replace

use Data/data_share_foregin_2019, clear
rename Ssyk4 SSYK2012
merge 1:1 SSYK2012 using "Data/nyckel_ssyk96_ssyk2012_20160905.dta"
drop _merge 
keep Foreign SSYK96
rename Foreign Foreign_2019
duplicates drop SSYK96, force
merge 1:1 SSYK96 using "Data/tmp_foregin_2007"
erase Data/tmp_foregin_2007.dta
keep if _merge == 3
drop _merge
rename Foreign Foreign_2007

gen change_foreign_2007_2019 = Foreign_2019 - Foreign_2007 
order SSYK96 Foreign_2007 Foreign_2019 change_foreign_2007_2019

xtile quartile = change_foreign_2007_2019, nq(4)
summarize change_foreign_2007_2019, detail
gen change_foreign_q1 = (quartile == 1)
gen change_foreign_q2 = (quartile == 2)
gen change_foreign_q3 = (quartile == 3)
gen change_foreign_q4 = (quartile == 4)
bys quartile: sum change_foreign_2007_2019
drop quartile
rename  SSYK96 SSYK4
destring SSYK4, replace
save Data/tmp_change_foreign, replace

* Now make four graphs based on the quartiles
use Data/data_figures_annonser_lang_JHR, clear
merge m:1 SSYK4 using Data/tmp_change_foreign
erase Data/tmp_change_foreign.dta
keep if _merge == 3

gen full_sample = 1

local dummies full_sample change_foreign_q1 change_foreign_q2 change_foreign_q3 change_foreign_q4

foreach dummy in `dummies' {
    preserve
    
    keep if `dummy' == 1

    collapse language_req_mentioned, by(year)
    drop if year == .
    sort year
    
    * Create the graph
    local title_text = ""
    if "`dummy'" == "full_sample" {
        local title_text = "`title_text' (Full Sample)"
    } 
	else {
        local title_text = substr("`dummy'", -2, 2)
    }
	
	if "`title_text'" == "q1" {
    local panel_title "Quartile 1"
	}
	else if "`title_text'" == "q2" {
		local panel_title "Quartile 2"
	}
	else if "`title_text'" == "q3" {
		local panel_title "Quartile 3"
	}
	else if "`title_text'" == "q4" {
		local panel_title "Quartile 4"
	}

    twoway connected language_req_mentioned year, ///
    title("`panel_title'", color(black) ) ///
    ytitle("") xtitle("") ///
    ylabel(,nogrid) graphregion(color(gs16)) ///
    xlabel(,nogrid) ///
    mcolor(black) lcolor(black)
    
    * Save the graph
    graph save "Stata output/`dummy'", replace
    restore
}

graph combine "Stata output/change_foreign_q1.gph" "Stata output/change_foreign_q2.gph" ///
              "Stata output/change_foreign_q3.gph" "Stata output/change_foreign_q4.gph", ycommon ///
              cols(2) title("") ///
              graphregion(color(white))
*******************************************************************************
*** Table B1. Language requirements in job ads and immigration 
*** (year x region x occuption) 
*******************************************************************************

use Data/data_figures_annonser_lang_time_region_JHR, clear

gen share_immig = 100 * (immig / numworkers)

collapse language_req_mentioned immig share_immig, by(year region ssyk3_96)

tab ssyk3_96, gen(sssyk3_96)
tab year, gen(yyear) 
tab region, gen(rregion) 
gen reg_ssyk_year = string(ssyk3_96) + string(year) + string(region)
replace immig = immig/1000

bysort region ssyk3_96 (year): gen immig_lag1 = immig[_n-1] 
bysort region ssyk3_96 (year): gen net_immig = immig - immig[_n-1]

areg language_req_mentioned immig yyear* rregion*, absorb(ssyk3_96) robust
areg language_req_mentioned immig_lag1  yyear* rregion*, absorb(ssyk3_96) robust

*******************************************************************************
*** Table B2. Shifts in language requirements in job ads and immigration 
*** post 2015 
*******************************************************************************
* Uses the same data as Table B1
gen post_2015 = (year > 2015)  // Create dummy for post-2015 period
gen immig_lag1_post2015 = immig_lag1 * post_2015  // Interaction term
areg language_req_mentioned immig_lag1 immig_lag1_post2015 yyear* rregion*, absorb(ssyk3_96) robust

*******************************************************************************
*** Figure B6. Language vs. educational requirements in job ads over time
*******************************************************************************

use Data/data_figures_annonser_edu_JHR, clear
collapse utbildning_req_mentioned, by(year)
drop if year == .
sort year

twoway connected utbildning_req_mentioned year, ///
title("B: Education", color(black)) ///
ytitle("") xtitle("") ///
yscale(range(0 .40)) ///
ylabel(,nogrid) graphregion(color(gs16)) ///
xlabel(,nogrid) ///
mcolor(black) lcolor(black)
graph save "Stata output\UTB All utb.ghp", replace

graph combine "Stata output\All adds Swedish.ghp" "Stata output\UTB All utb.ghp" , rows(1) xcommon ycommon ///
graphregion(color(white))




